/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.RealStateType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class RealEstateTypeDAO {

    private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

     public ArrayList<RealStateType> GetAll() {
        ArrayList<RealStateType> listRealStateType = new ArrayList<RealStateType>();
        RealStateType realEstateType = null;
        try {

            strCommand = "select * from tbl_RealStateType WHERE realStateTypeStatus = 1 OR realStateTypeStatus = 0";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
           realEstateType=new RealStateType();

           realEstateType.setRealStateTypeID(rs.getInt("realStateTypeID"));
           realEstateType.setRealStateTypeName(rs.getString("realStateTypeName"));
           realEstateType.setRealStateTypeStatus(rs.getInt("realStateTypeStatus"));

           listRealStateType.add(realEstateType);

            }
            return listRealStateType;
        } catch (SQLException ex) {
            Logger.getLogger(RealEstateTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listRealStateType;
    }

     public RealStateType GetByID(int realStateTypeID) {

        RealStateType realEstateType = new RealStateType();
        try {

            strCommand = "select * from tbl_RealStateType WHERE realStateTypeID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, realStateTypeID);
            rs = ps.executeQuery();
            while (rs.next()) {
            realEstateType.setRealStateTypeID(rs.getInt("realStateTypeID"));
            realEstateType.setRealStateTypeName(rs.getString("realStateTypeName"));
            realEstateType.setRealStateTypeStatus(rs.getInt("realStateTypeStatus"));
            }
            return realEstateType;
        } catch (SQLException ex) {
            Logger.getLogger(RealEstateTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return realEstateType;
    }

     public boolean editRealStateType(RealStateType objRealStateType) {
        //UPDATE tbl_Banks
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_RealStateType SET realStateTypeName = ? ,realStateTypeStatus = ? WHERE realStateTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objRealStateType.getRealStateTypeName());
            ps.setInt(2, objRealStateType.getRealStateTypeStatus());
            ps.setInt(3, objRealStateType.getRealStateTypeID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteRealStateType(int realStateTypeID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_RealStateType WHERE realStateTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, realStateTypeID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean AddRealStateType(String realStateTypeName) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_RealStateType (realStateTypeName,realStateTypeStatus) VALUES (?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, realStateTypeName);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(RealEstateTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
